//
// Created by zing on 3/28/2020.
//

#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    int fd;
    char buf[1024];
    fd = open("test.txt", O_RDWR);
    if (fd < 0) {
        perror("open file error");
        return EXIT_FAILURE;
    }
    ssize_t i = read(fd, buf, 8);
    if (i < 0) {
        perror(strerror(errno));
        return EXIT_FAILURE;
    }
    printf("读取了%zd个字符\n", i);
    char data[i];
    for (int j = 0; j < i; ++j) {
        data[j] = buf[j];
    }
    data[i] = '\0';
    printf("%s\n", data);
    off_t i1 = lseek(fd, 1, SEEK_SET);
    if (i1 < 0) {
        perror(strerror(errno));
        return EXIT_FAILURE;
    }

    i = read(fd, buf, 5);
    if (i < 0) {
        perror(strerror(errno));
        return EXIT_FAILURE;
    }
    printf("读取了%zd个字符\n", i);
    for (int j = 0; j < i; ++j) {
        data[j] = buf[j];
    }
    data[i] = '\0';
    printf("%s\n", data);
    close(fd);
    return 0;
}
